package gui.view;

import generated.business.world.WorldService;
import generated.domain.user.User;
import gui.action.KickAction;
import gui.entity.UserEntity;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.part.ViewPart;

import vworld.Client;

public class ConnectedUsersView extends ViewPart {
	public static final String ID = "gui.view.ConnectedUsersView";
	private ListViewer connectedUsersViewer;
	private KickAction kickAction;

	@Override
	public void createPartControl(Composite parent) {
		connectedUsersViewer = new ListViewer(parent, SWT.BORDER);
		connectedUsersViewer.setLabelProvider(new ListLabelProvider());
		connectedUsersViewer.setContentProvider(new ContentProvider());
		connectedUsersViewer.setInput(createModel());

		makeActions();
		hookContextMenu();
		hookDoubleClickAction();

		getSite().setSelectionProvider(connectedUsersViewer);
	}

	private Object createModel() {
		return Client.WORLD_SERVICE;
	}

	@Override
	public void setFocus() {
		connectedUsersViewer.getControl().setFocus();
	}

	class ContentProvider implements IStructuredContentProvider {
		public void dispose() {
		}

		public Object[] getElements(Object inputElement) {
			List<UserEntity> entityList = new ArrayList<UserEntity>();
			for (User u : ((WorldService) inputElement).getConnectedUsers()) {
				if (!u.role.equalsIgnoreCase("administrator"))
					entityList.add(new UserEntity(u));
			}
			return entityList.toArray(new UserEntity[entityList.size()]);
		}

		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
	}

	class ListLabelProvider extends LabelProvider {
		@Override
		public Image getImage(Object element) {
			return null;
		}

		@Override
		public String getText(Object element) {
			return ((UserEntity) element).getUser().name;
		}
	}

	private void makeActions() {
		kickAction = new KickAction(connectedUsersViewer, "Kick");
	}

	private void hookContextMenu() {
		MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				ConnectedUsersView.this.fillContextMenu(manager);
			}
		});
		Menu menu = menuMgr
				.createContextMenu(connectedUsersViewer.getControl());
		connectedUsersViewer.getControl().setMenu(menu);
		getSite().registerContextMenu(menuMgr, connectedUsersViewer);
	}

	private void fillContextMenu(final IMenuManager manager) {
		ISelection selection = connectedUsersViewer.getSelection();
		if (selection != null) {
			manager.add(kickAction);
		}
		manager.add(new Separator());
	}

	private void hookDoubleClickAction() {
		connectedUsersViewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event
						.getSelection();
				if (!selection.isEmpty()) {
					Object object = selection.getFirstElement();
					if (object instanceof UserEntity) {
						kickAction.run();
					}
				}
			}
		});
	}

	/**
	 * @return the connectedUsersViewer
	 */
	public ListViewer getConnectedUsersViewer() {
		return connectedUsersViewer;
	}
}